package com.lzp.appexp

import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 4/30/21
 */

interface IRent {
    fun rentRoom(name: String,room: String,roomer: String)
}

class RealRent() : IRent {
    override fun rentRoom(name: String,room: String,roomer: String) {
        println("中介 $name 想把房子 $room 租给租客$roomer")
    }
}

fun main() {
    val realRent = RealRent() as IRent
    val handle = RentHandle(realRent)
    val proxy = Proxy.newProxyInstance(realRent.javaClass.classLoader, arrayOf<Class<*>>(IRent::class.java), handle) as IRent
    proxy.rentRoom("张三", "303", "李四")
}

class RentHandle(private val target: Any) : InvocationHandler {
    override fun invoke(proxy: Any, method: Method?, args: Array<Any>?): Any? {
        method?.invoke(target, *args.orEmpty())
        println("房屋管理系统：检查房子${args?.get(1).toString()}是否已经租出去")
        println("房屋管理系统：房子${args?.get(1).toString()}尚未租出")
        println("中介${args?.get(0).toString()} 成功出租房子${args?.get(1).toString()}给 ${args?.get(2).toString()}")
        return null
    }
}

